{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c1ede8c3",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "import h5py\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import matplotlib.image as mpimg\n",
    "import os\n",
    "import os.path as osp\n",
    "import imageio.v2 as imageio\n",
    "\n",
    "from scipy.stats import describe"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7a5f5884",
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "import cv2\n",
    "def imread_cv2(path, options=cv2.IMREAD_COLOR):\n",
    "    \"\"\"Open an image or a depthmap with opencv-python.\"\"\"\n",
    "    if path.endswith((\".exr\", \"EXR\")):\n",
    "        options = cv2.IMREAD_ANYDEPTH\n",
    "    img = cv2.imread(path, options)\n",
    "    if img is None:\n",
    "        raise IOError(f\"Could not load image={path} with {options=}\")\n",
    "    if img.ndim == 3:\n",
    "        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n",
    "    return img"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "709073f5",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "# Scannet++ depth dataset\n",
    "s = 0\n",
    "# List of file paths\n",
    "root = \"/lc/data/3D/vkitti/Scene18/30-deg-left/Camera_0\"\n",
    "file_paths = sorted([f for f in os.listdir(root) if f.endswith(\".jpg\")])\n",
    "\n",
    "cols = 2\n",
    "rows = 10\n",
    "interval = 1\n",
    "# Create a figure with 2 columns and 10 rows,dpi=100\n",
    "fig, axs = plt.subplots(rows, cols, figsize=(12, rows*2))\n",
    "\n",
    "# Iterate over the file paths and display the images\n",
    "for i, file_path in enumerate(file_paths[s:rows*cols*interval//2+s:interval]):\n",
    "    rgb_path = osp.join(root, file_path)\n",
    "    depth_path = rgb_path.replace(\"_rgb.jpg\", \"_depth.png\")\n",
    "    cam_path = rgb_path.replace(\"_rgb.jpg\", \"_cam.npz\")\n",
    "    img = imread_cv2(rgb_path, cv2.IMREAD_COLOR)\n",
    "    depth = imread_cv2(depth_path, cv2.IMREAD_UNCHANGED)/100\n",
    "    cam_file = np.load(cam_path)\n",
    "    desc = f'{depth.min()}, {depth.max()}, {depth.mean()}'\n",
    "    cam_desc = f'{cam_file['camera_pose'][:3, -1]}'\n",
    "    axs[i * 2 // cols , i * 2 % cols ].imshow(img)\n",
    "    axs[i * 2 // cols , i * 2 % cols].axis(\"off\")  # Hide axis ticks\n",
    "    axs[i * 2 // cols , i * 2 % cols].set_title(cam_desc)\n",
    "    axs[i * 2 // cols , i * 2 % cols + 1].imshow(depth)\n",
    "    axs[i * 2 // cols , i * 2 % cols + 1].axis(\"off\")  # Hide axis ticks\n",
    "    axs[i * 2 // cols , i * 2 % cols + 1].set_title(desc)\n",
    "\n",
    "    # axs[i * 2 // cols , i * 2 % cols ].imshow(img)\n",
    "    # axs[i * 2 // cols , i * 2 % cols].axis(\"off\")  # Hide axis ticks\n",
    "    # axs[i * 2 // cols , i * 2 % cols].set_title(cam_desc)\n",
    "    # axs[i * 2 // cols , i * 2 % cols + 1].imshow(depth)\n",
    "    # axs[i * 2 // cols , i * 2 % cols + 1].axis(\"off\")  # Hide axis ticks\n",
    "    # axs[i * 2 // cols , i * 2 % cols + 1].set_title(file_path.split('_')[0]+\"_depth\")\n",
    "\n",
    "plt.show()\n",
    "# continious pose in driving car\n",
    "# poses correct"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cd3e4d32",
   "metadata": {},
   "outputs": [],
   "source": [
    "def compare(depth, depth1):\n",
    "    mask = depth != depth1\n",
    "    if mask.sum() == 0:\n",
    "        return 0, 0\n",
    "    diff = np.abs(depth[mask].astype(np.int32) - depth1[mask])\n",
    "    return diff.max(), diff.sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "99594ce0",
   "metadata": {},
   "outputs": [],
   "source": [
    "# m, s = compare(depth1, depth)\n",
    "# print(m, s)\n",
    "# print(m>1)\n",
    "# depth[depth==65535] = 0\n",
    "# print(depth.max())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b00f3fcd",
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2\n",
    "# Scannet++ depth dataset\n",
    "s = 0\n",
    "# List of file paths\n",
    "subscenes = ['15-deg-left', '15-deg-right', '30-deg-right']\n",
    "root = f\"/lc/data/3D/vkitti/Scene06/{subscenes[0]}/Camera_0\"\n",
    "file_paths = sorted([f for f in os.listdir(root) if f.endswith(\".jpg\")])\n",
    "\n",
    "cols = 3\n",
    "rows = 6\n",
    "interval = 1\n",
    "# Create a figure with 2 columns and 10 rows,dpi=100\n",
    "fig, axs = plt.subplots(rows, cols, figsize=(12, rows*2))\n",
    "\n",
    "# Iterate over the file paths and display the images\n",
    "for i, file_path in enumerate(file_paths[s:rows*cols*interval//3+s:interval]):\n",
    "    img = mpimg.imread(osp.join(root, file_path))\n",
    "    img1 = mpimg.imread(osp.join(root.replace(subscenes[0],subscenes[1]), file_path))\n",
    "    img2 = mpimg.imread(osp.join(root.replace(subscenes[0],subscenes[2]), file_path))\n",
    "    axs[i * 3 // cols , i * 3 % cols ].imshow(img)\n",
    "    axs[i * 3 // cols , i * 3 % cols].axis(\"off\")  # Hide axis ticks\n",
    "    axs[i * 3 // cols , i * 3 % cols].set_title(subscenes[0])\n",
    "    axs[i * 3 // cols , i * 3 % cols + 1].imshow(img1)\n",
    "    axs[i * 3 // cols , i * 3 % cols + 1].axis(\"off\")  # Hide axis ticks\n",
    "    axs[i * 3 // cols , i * 3 % cols + 1].set_title(subscenes[1])\n",
    "    axs[i * 3 // cols , i * 3 % cols + 2].imshow(img2)\n",
    "    axs[i * 3 // cols , i * 3 % cols + 2].axis(\"off\")  # Hide axis ticks\n",
    "    axs[i * 3 // cols , i * 3 % cols + 2].set_title(subscenes[2])\n",
    "    # \n",
    "    depth_f = file_path.replace(\"rgb.jpg\",\"depth.png\")\n",
    "    depth = cv2.imread(osp.join(root, depth_f), cv2.IMREAD_ANYDEPTH)\n",
    "    depth1 = cv2.imread(osp.join(root.replace(subscenes[0],subscenes[1]), depth_f), cv2.IMREAD_ANYDEPTH)\n",
    "    depth2 = cv2.imread(osp.join(root.replace(subscenes[0],subscenes[2]), depth_f), cv2.IMREAD_ANYDEPTH)\n",
    "    # assert compare(depth, depth1)[0] <= 1\n",
    "    # assert compare(depth2, depth1)[0] <= 1\n",
    "    # assert compare(depth, depth2)[0] <= 1\n",
    "\n",
    "plt.show()\n",
    "# continious pose in driving car\n",
    "# the depth of subscenes (clone, fog, morning, 'overcast', 'rain', 'sunset') in the same scene are the same"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2616181c",
   "metadata": {},
   "outputs": [],
   "source": [
    "file_path = file_paths[0]\n",
    "img = mpimg.imread(osp.join(root, file_path))\n",
    "print(img.shape)\n",
    "cam_f = file_path.split('_')[0] + '_cam.npz'\n",
    "cam = np.load(osp.join(root, cam_f))\n",
    "print(cam.keys())\n",
    "print(\"intrinsics\", cam['camera_intrinsics'].shape)\n",
    "print(cam['camera_intrinsics'])\n",
    "print(\"pose\", cam['camera_pose'].shape)\n",
    "print(cam['camera_pose'])\n",
    "\n",
    "depth = mpimg.imread(osp.join(root, file_path.replace(\"rgb.jpg\",\"depth.png\")))\n",
    "print(depth.shape, depth.dtype) \n",
    "print(describe(depth, axis=None))\n",
    "print(describe(depth*65535, axis=None))\n",
    "# import cv2\n",
    "# cv_depth = cv2.imread(osp.join(root, file_path.replace(\"rgb.jpg\",\"depth.png\")), cv2.IMREAD_ANYDEPTH)\n",
    "# print(describe(cv_depth, axis=None))\n",
    "# print(describe(cv_depth/65535, axis=None))\n",
    "# (375, 1242, 3)\n",
    "# intrinsics (3, 3)\n",
    "# [[725.0087   0.     620.5   ]\n",
    "#  [  0.     725.0087 187.    ]\n",
    "#  [  0.       0.       1.    ]]\n",
    "# pose (4, 4)\n",
    "# [[ 9.59794104e-01  8.06020573e-03  2.80589014e-01  6.04366541e-01]\n",
    "#  [-1.23195788e-02  9.99834001e-01  1.34195965e-02 -1.14650032e+02]\n",
    "#  [-2.80434251e-01 -1.63367912e-02  9.59734201e-01  9.58537221e-01]\n",
    "#  [ 0.00000000e+00  0.00000000e+00  0.00000000e+00  1.00000000e+00]]\n",
    "# (375, 1242) float32"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "vggt",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
